package controllers;

import java.util.List;
import java.lang.Long;

import models.Formation;
import models.User;
import play.data.validation.Valid;
import play.i18n.Messages;
import play.mvc.*;

@With(Secure.class)
public class Formations extends Controller {

    public static User user;

    @Before
    static void setConnectedUser() {
        if (Security.isConnected()) {
            User user = User.find("byUsername", Security.connected()).first();
            renderArgs.put("user", user);


        }
    }

    public static void list() {
        List<Formation> entities = models.Formation.all().fetch();
        render(entities);
    }

    public static void show(Long id) {
        Formation entity = Formation.findById(id);
        render(entity);

    }

    public static void create() {
        render();
    }

    public static void edit(Long id) {
        Formation entity = Formation.findById(id);
        render(entity);
    }

    public static void delete(Long id) {
        Formation entity = Formation.findById(id);
        entity.delete();
        flash.success(Messages.get("Formations.deleted"));
        Formations.list();
    }

    public static void save(@Valid Formation entity) {
        if (validation.hasErrors()) {
            flash.error(Messages.get("validation"));
            render("@create", entity);
        }
        entity.save();
        flash.success(Messages.get("Formations.created"));
        Formations.list();

    }

    public static void update(@Valid Formation entity) {
        if (validation.hasErrors()) {
            flash.error(Messages.get("validation"));
            render("@edit", entity);
        }

        entity = entity.merge();

        entity.save();
        flash.success(Messages.get("Formations.updated"));
        Formations.list();

    }

    public static void maFormation() {
        Integer id = 1;
        Long idL = id.longValue();
        Formation entity = Formation.findById(idL);
        render(entity);
    }

    public static void demande() {
        render();
    }

    public static void examen() {
        render();
    }

    public static void send() {
        flash.success(Messages.get("FileSend"));
        render("@admin.index");
    }
}
